/*
 * Copyright (c) 2019 Wormpex.com. All Rights Reserved.
 */

package com.sk.classload;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author ke.su date: 2019-11-19 Time: 下午9:54
 */
public class MyClassLoaderTest2 extends ClassLoader {
    public MyClassLoaderTest2() {

    }

    @Override
    protected Class<?> findClass(String name) {
        System.out.println("aaaaaaa");
        byte[] data = this.loadClassData(name);
        return this.defineClass(name, data, 0, data.length);
    }

    private byte[] loadClassData(String className) {
        try (InputStream is = new FileInputStream(new File(className));
             ByteArrayOutputStream bOut = new ByteArrayOutputStream()) {

            int ch = 0;
            while (-1 != (ch = is.read())) {
                bOut.write(ch);
            }
            return bOut.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}
